package com.ruoyi.project.agreement.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.agreement.domain.AgreementInfo;
import com.ruoyi.project.agreement.service.IAgreementInfoService;
import com.ruoyi.project.custom.domain.CustomInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/agreement")
public class AgreementInfoController extends BaseController {
    @Autowired
    private IAgreementInfoService agreementService;

    @GetMapping("/selectAgreementList")
    public TableDataInfo selectAgreementList(AgreementInfo agreementInfo)
    {
        startPage();
        List<AgreementInfo> list = agreementService.selectAgreementList(agreementInfo);
        return getDataTable(list);
    }

    @GetMapping("/selectMaxEndDateAgreementList")
    public TableDataInfo selectMaxEndDateAgreementList(AgreementInfo agreementInfo)
    {
        startPage();
        List<AgreementInfo> list = agreementService.selectMaxEndDateAgreement(agreementInfo);
        return getDataTable(list);
    }

    @GetMapping("/selectValidAgreementList")
    public AjaxResult selectValidAgreementList(AgreementInfo agreementInfo)
    {
        List list = agreementService.selectValidAgreementList(agreementInfo);
        return AjaxResult.success(list);
    }

    @GetMapping("/selectAgreementByCustomId/{customId}")
    public AjaxResult selectAgreementByCustomId(@PathVariable String customId)
    {
        return AjaxResult.success(agreementService.selectAgreementByCustomId(customId));
    }

    @GetMapping("/selectAgreementById/{id}")
    public AjaxResult selectAgreementById(@PathVariable String id)
    {
        return AjaxResult.success(agreementService.selectAgreementById(id));
    }

    @PostMapping("")
    @Log(title = "新增合同", businessType = BusinessType.INSERT)
    public AjaxResult insert(@RequestBody AgreementInfo agreementInfo)
    {
        return agreementService.insertAgreement(agreementInfo);
    }

    @PutMapping("")
    @Log(title = "确认合同", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody AgreementInfo agreementInfo)
    {
        return agreementService.updateAgreement(agreementInfo);
    }

    @DeleteMapping("/{id}")
    @Log(title = "删除合同", businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable String id)
    {
        return toAjax(agreementService.deleteAgreement(id));
    }

    @DeleteMapping("/deleteAgreements/{ids}")
    @Log(title = "批量删除合同", businessType = BusinessType.DELETE)
    public AjaxResult deleteAgreements(@PathVariable String[] ids)
    {
        return toAjax(agreementService.deleteAgreements(ids));
    }

    @Log(title = "导出合同信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AgreementInfo agreementInfo)
    {
        List<AgreementInfo> list = agreementService.selectMaxEndDateAgreement(agreementInfo);
        ExcelUtil<AgreementInfo> util = new ExcelUtil<AgreementInfo>(AgreementInfo.class);
        return util.exportExcel(list, "合同信息");
    }
}
